package com.hxut.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hxut.common.dto.SysMenuDto;
import com.hxut.entity.SysMenu;
import com.hxut.entity.SysUser;
import com.hxut.mapper.SysMenuMapper;
import com.hxut.service.SysMenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hxut.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author MR.S
 * @since 2022-06-21
 */
@Service
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> implements SysMenuService {

    @Autowired
    private SysMenuMapper sysMenuMapper;

    @Autowired
    private SysUserService sysUserService;

    @Override
    public List<SysMenuDto> getCurrentUserNav() {
        String username = (String) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        SysUser sysUser = sysUserService.getByUsername(username);

        //获取导航菜单信息

        //根据uses_id查询关联的菜单id
        List<Long> menuIds = sysMenuMapper.getMenuIds(sysUser.getId());
        //根据菜单id找到所有的菜单信息
        List<SysMenu> sysMenus = this.listByIds(menuIds);

        //将菜单信息转换为树形结构
        List<SysMenu> menuTree = buildMenuTree(sysMenus);

        //转换为MenuDto结构

        return convert(menuTree);
    }

    @Override
    public List<SysMenu> tree() {
        //根据排序字段排序，并获得所有菜单信息
        List<SysMenu> menus = this.list(new QueryWrapper<SysMenu>().orderByAsc("orderNum"));
        //转换成树状结构
        return this.buildMenuTree(menus);
    }

    /**
     * @description:  转换为Dto类型
     * @param menuTree
     * @return: java.util.List<com.hxut.common.dto.SysMenuDto>
     * @author: MR.孙
     * @date: 2022/7/11 20:01
    */
    private List<SysMenuDto> convert(List<SysMenu> menuTree) {
        List<SysMenuDto> menuDtos = new ArrayList<>();
        menuTree.forEach(menu->{
            SysMenuDto menuDto = new SysMenuDto();
            menuDto.setId(menu.getId());
            menuDto.setName(menu.getPerms());
            menuDto.setTitle(menu.getName());
            menuDto.setComponent(menu.getComponent());
            menuDto.setPath(menu.getPath());

            //子节点调用当前方法进行再次转换
            if(menu.getChildren().size()>0) {
                menuDto.setChildren(this.convert(menu.getChildren()));
            }
            menuDtos.add(menuDto);
        });
        return menuDtos;
    }

    /**
     * @description:  转换为树形结构
     * @param sysMenus
     * @return: java.util.List<com.hxut.entity.SysMenu>
     * @author: MR.孙
     * @date: 2022/7/11 20:01
    */
    private List<SysMenu> buildMenuTree(List<SysMenu> sysMenus) {
        List<SysMenu> parentMenu = new ArrayList<>();
        for(SysMenu menu : sysMenus){
           for(SysMenu m : sysMenus){
               //找到各个父节点的孩子(子菜单)
              if(menu.getId()==m.getParentId()){
                menu.getChildren().add(m);
              }
           }
           //提取父菜单下的所有信息
            if(menu.getParentId()==0){
                parentMenu.add(menu);
            }
        }

        return parentMenu;
    }


}
